#include<stdio.h>
#include<unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define ONE   (1<<0)

#define TWO   (1<<1)
#define THREE (1<<2)
#define FOUR  (1<<3)

#define FILE_NAME(number) "log.txt"#number
/*void show(int flag)
{
  if(flag&ONE) 
  {
    printf("one\n");
    printf("---------------\n");
  }
  if(flag&TWO) {
    printf("two\n");
    printf("---------------\n");}

  if(flag&THREE) 
  {
    printf("three\n");
    printf("---------------\n");
  }
  if(flag&FOUR) {
    printf("four\n");
    printf("---------------\n");
  }
}
*/ 
int main()
{

  printf("stdin->fd : %d\n",stdin->_fileno);
  printf("stdout->fd : %d\n",stdout->_fileno);
  printf("stderror->fd : %d\n",stderr->_fileno);
  umask(0);  //设置最初始得umake的值,这里改的是子进程继承下来的umask而不是shell的umask
  // w  ->  O_WRONLY | O_CREAT |O_TRUNC    O_TRUNC 是清空之前的内容
  int fp0 = open(FILE_NAME(0),O_WRONLY | O_CREAT |O_TRUNC ,0666);
  int fp1 = open(FILE_NAME(1),O_WRONLY | O_CREAT |O_TRUNC ,0666);
  int fp2 = open(FILE_NAME(2),O_WRONLY | O_CREAT |O_TRUNC ,0666);
  int fp3 = open(FILE_NAME(3),O_WRONLY | O_CREAT |O_TRUNC ,0666);
  int fp4 = open(FILE_NAME(4),O_WRONLY | O_CREAT |O_TRUNC ,0666);
  printf("fd0 : %d\n",fp0);
  printf("fd1 : %d\n",fp1);
  printf("fd2 : %d\n",fp2);
  printf("fd3 : %d\n",fp3);
  printf("fd4 : %d\n",fp4);
 // int fp = open(FILE_NAME,O_WRONLY | O_CREAT |O_APPEND ,0666); //追加数据源

//   int fp = open(FILE_NAME,O_RDONLY);  // 只读
/*  if(fp<0)
  {
    perror("open");
    return 1;
  }
*/  
 /* //读数据
  char bufferRead[1024];
  ssize_t num = read(fp,bufferRead,sizeof(bufferRead)-1);
  if(num>0)
    bufferRead[num]=0;  //0  '\0', NULL -->0
  printf("%s\n",bufferRead);
  
  printf("fd : %d\n",fp);
*/ 

  //写入数据
  /*
  int cnt=5;
  char buffer[64];
  while(cnt--)
  {
    //格式化写 操作系统只关心写入二进制文件，所以一般采用sprintf
    sprintf(buffer,"%s  %d\n","hello wrold",cnt);
    // 以\0作为字符串的结尾是C语言的规定，与文件无关，写入的时候不需要strlen+1
    write(fp,buffer,strlen(buffer));  //在这里不用＋1，不需要把/n写入文件中，因为/n是c语言的分隔符，不是操作系统的

  }
  */
  close(fp0);
  close(fp1);
  close(fp2);
  close(fp3);
  close(fp4);
  return 0;





/*
  show(ONE);

  show(TWO|ONE);
  show(THREE|TWO);
  show(FOUR|TWO|THREE|ONE);

*/ 


  // 以W的方式打开，c会自动清空内部数据
  // 666 &~ umask
  //FILE* fp=fopen(FILE_NAME,"w");  // w  r  a 
 // int cnt=5;
 // FILE* fp=fopen(FILE_NAME,"r");  // w  r  a 
  
 /* FILE* fp=fopen(FILE_NAME,"a");  // w  r  a 
  if(fp==NULL)
  {
    perror("error");
    return 1;
  }

  char buffer[64];

  
  while(cnt--)
  {
    fprintf(fp,"%s:%d\n","hello bit",cnt);

  }
  */
  /*
  while(fgets(buffer,sizeof(buffer)-1,fp)!=NULL)
  {
    buffer[strlen(buffer)-1]=0;
    puts(buffer);
  }
*/ 
  
  /*
  while(cnt--)
  {
    fprintf(fp,"%s:%d\n","hello bit",cnt);

  }
  */ 
 // fclose(fp);
  return 0;
}
